﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL
Imports Boogaloo.Admin.UI.Controls

Namespace UI.UserControls


    Partial Public Class Site_PageSettings
        Inherits CoreControlBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Groove.admin_current_node IsNot Nothing Then
                If Not Page.IsPostBack Then
                    lht_change_image.CommandArgument = "main_photo"


                    lhddl_child_layout.dropdownlist.DataSource = Groove.admin_root.layouts
                    lhddl_child_layout.dropdownlist.DataTextField = "menu_item_text"
                    lhddl_child_layout.dropdownlist.DataValueField = "node_id"
                    lhddl_child_layout.dropdownlist.DataBind()

                    lhddl_child_sort.dropdownlist.DataSource = ChildNodeSort.GetChildNodeSortOrders()
                    lhddl_child_sort.dropdownlist.DataTextField = "name"
                    lhddl_child_sort.dropdownlist.DataValueField = "child_sort_type_id"
                    lhddl_child_sort.dropdownlist.DataBind()

                    With lhddl_sitemap_url_priority.dropdownlist
                        .Items.Add("1.0")
                        .Items.Add("0.9")
                        .Items.Add("0.8")
                        .Items.Add("0.7")
                        .Items.Add("0.6")
                        .Items.Add("0.5")
                        .Items.Add("0.4")
                        .Items.Add("0.3")
                        .Items.Add("0.2")
                        .Items.Add("0.1")
                        .Items.Add("0.0") ' will not appear in the sitemap
                    End With

                    With lhddl_sitemap_change_frequency.dropdownlist
                        .Items.Add(SitemapChangeFrequency.hourly.ToString)
                        .Items.Add(SitemapChangeFrequency.daily.ToString)
                        .Items.Add(SitemapChangeFrequency.weekly.ToString)
                        .Items.Add(SitemapChangeFrequency.monthly.ToString)
                        .Items.Add(SitemapChangeFrequency.yearly.ToString)
                        .Items.Add(SitemapChangeFrequency.never.ToString)
                    End With

                    Dim components As List(Of Component) = Component.GetComponentsManageNode(Groove.admin_root.root_id)
                    If components.Count <= 1 Then
                        lhddl_child_component.Visible = False
                    Else
                        lhddl_child_component.Visible = True
                        lhddl_child_component.dropdownlist.DataSource = components
                        lhddl_child_component.dropdownlist.DataTextField = "name"
                        lhddl_child_component.dropdownlist.DataValueField = "component_id"
                        lhddl_child_component.dropdownlist.DataBind()

                    End If


                    hyp_tagsgroups.NavigateUrl = String.Format("{0}TagsGroups.aspx?node={1}", Groove.path_admin_host, Groove.admin_current_node.node_id.ToString)

                    BindControl()

                End If
            End If
        End Sub


        Private Sub BindControl()

            With Groove.admin_current_node

                ' Top display toggle
                ' ----------------------
                If .display Then
                    lbl_display.Text = "Display"
                    lb_display_toggle.Text = "Don't Display This Page"
                    lbl_display.CssClass = "displayStatus"
                Else
                    lbl_display.Text = "Not Display"
                    lb_display_toggle.Text = "Display This Page"
                    lbl_display.CssClass = "displayStatus no"
                End If
                If .is_default_node Then lb_display_toggle.Visible = False

                ' Set the images
                ' -----------------------


                If lht_change_image.CommandArgument = "main_photo" Then
                    If Not String.IsNullOrEmpty(.main_photo) Then
                        img.Alt = "Main Image"
                        img.Src = .main_photo
                    Else
                        img.Alt = "The Main Image has not been set for this page"
                        img.Src = "nomainimage.jpg"
                    End If                    
                    lb_view_image.Text = "View Thumbnail"
                    lbl_imagetype.Text = "Main Image"
                    lht_change_image.Text = .main_photo
                Else
                    If Not String.IsNullOrEmpty(.thumbnail) Then
                        img.Alt = "Thumbnail"
                        img.Src = .thumbnail
                    Else
                        img.Src = "nothumbnail.jpg"
                        img.Alt = "A Thumbnail has not been set for this page"
                    End If

                    img.Alt = "Thumbnail"
                    lbl_imagetype.Text = "Thumbnail"
                    lb_view_image.Text = "View Main Image"
                    lht_change_image.Text = .thumbnail
                End If

                ' Publish Date
                ' ----------------------
                lbl_publish_date.Text = String.Format("<em>Set the publish date</em> <span>{0}</span>", .publish_date.ToShortDateString)
                txt_publish_date.Text = .publish_date.ToShortDateString
                hyp_cancel.NavigateUrl = Request.Url.PathAndQuery


                ' Document Meta
                ' ----------------------

                If Not String.IsNullOrEmpty(.page_title) Then lht_page_title.label = String.Format("<strong class='settingsSubTitle'>This is the page title:</strong> <span>{0}</span>", .page_title)
                lht_page_title.Text = .page_title

                If Not String.IsNullOrEmpty(.meta_keywords) Then lht_meta_keywords.label = String.Format("<strong class='settingsSubTitle'>These are the Keywords:</strong> <span>{0}</span>", .meta_keywords)
                lht_meta_keywords.Text = .meta_keywords

                If Not String.IsNullOrEmpty(.meta_description) Then lht_meta_description.label = String.Format("<strong class='settingsSubTitle'>Here is the description:</strong> <span>{0}</span>", .meta_description)
                lht_meta_description.Text = .meta_description

                If Not String.IsNullOrEmpty(.meta_abstract) Then lht_meta_abstract.label = String.Format("<strong class='settingsSubTitle'>Here is the Meta Abstract:</strong> <span>{0}</span>", .meta_abstract)
                lht_meta_abstract.Text = .meta_abstract

                If Not String.IsNullOrEmpty(.meta_author) Then lht_meta_author.label = String.Format("<strong class='settingsSubTitle'>This is the author:</strong> <span>{0}</span>", .meta_author)
                lht_meta_author.Text = .meta_author

                If Not String.IsNullOrEmpty(.meta_robots) Then lht_meta_robots.label = String.Format("<strong class='settingsSubTitle'>Here is how You Want Search Engines (Robots) to Respond:</strong> <span>{0}</span>", .meta_robots)
                lht_meta_robots.Text = .meta_robots

                If Not String.IsNullOrEmpty(.meta_copyright) Then lht_meta_copyright.label = String.Format("<strong class='settingsSubTitle'>Here is the Copyright Information:</strong> <span>{0}</span>", .meta_copyright)
                lht_meta_copyright.Text = .meta_copyright


                Dim priorty As String = IIf(.sitemap_url_priority = 1, "1.0", String.Format("{0}", .sitemap_url_priority))
                lhddl_sitemap_url_priority.label = String.Format("<strong class='settingsSubTitle'>The XML Sitemap priority is set to:</strong> <span>{0}</span>", priorty)
                lhddl_sitemap_url_priority.dropdownlist.SelectedValue = priorty

                Select Case .sitemap_change_frequency
                    Case SitemapChangeFrequency.daily.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.daily.ToString
                    Case SitemapChangeFrequency.hourly.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.hourly.ToString
                    Case SitemapChangeFrequency.weekly.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.weekly.ToString
                    Case SitemapChangeFrequency.monthly.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.monthly.ToString
                    Case SitemapChangeFrequency.yearly.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.yearly.ToString
                    Case SitemapChangeFrequency.never.ToString.Substring(0, 1)
                        lhddl_sitemap_change_frequency.dropdownlist.SelectedValue = SitemapChangeFrequency.never.ToString
                End Select
                lhddl_sitemap_change_frequency.label = String.Format("<strong class='settingsSubTitle'>This Page is Modified This Often:</strong> <span>{0}</span>", lhddl_sitemap_change_frequency.dropdownlist.SelectedValue)

                ' Child Pages
                ' ---------------------
                lhddl_child_layout.dropdownlist.SelectedValue = .child_layout_node_id.ToString
                lhddl_child_layout.label = String.Format("<strong class='settingsSubTitle'>Here is the layout for sub-pages of this page:</strong> <span>{0}</span>", lhddl_child_layout.dropdownlist.SelectedItem.Text)

                lhddl_child_sort.dropdownlist.SelectedValue = .child_sort_type_id.ToString
                lhddl_child_sort.label = String.Format("<strong class='settingsSubTitle'>Change the criteria in which the subpages of this page will be sorted:</strong> <span>{0}</span>", lhddl_child_sort.dropdownlist.SelectedItem.Text)

                If lhddl_child_component.Visible Then
                    lhddl_child_component.dropdownlist.SelectedValue = .child_component_id.ToString
                    lhddl_child_component.label = String.Format("<strong class='settingsSubTitle'>Which extension should control the sub page:</strong> <span>{0}</span>", lhddl_child_component.dropdownlist.SelectedItem.Text)
                End If




                ' display settings
                ' ----------------------
                chk_display.Checked = .display
                If .is_default_node Then chk_display.Visible = False
                chk_display_in_menus.Checked = .display_in_menus
                chk_display_in_lists.Checked = .display_in_lists

                chk_managed_on_publish.Checked = .managed_on_publish
                If Groove.publish_admin_pending.Count > 0 Then
                    chk_managed_on_publish.Enabled = False
                    chk_managed_on_publish.Text &= " (publishes pending)"
                End If


                chk_archived.Checked = .archived
                chk_archived.Visible = False ' removed from intial version

                ' Anchor Settings
                ' ----------------------
                If Not String.IsNullOrEmpty(.menu_item_css) Then lht_menu_item_css.label = String.Format("<strong class='settingsSubTitle'>This is the menu item CSS class:</strong> <span>{0}</span>", .menu_item_css)
                lht_menu_item_css.Text = .menu_item_css

                If Not String.IsNullOrEmpty(.page_link_css) Then lht_page_link_css.label = String.Format("<strong class='settingsSubTitle'>This is the page link CSS class:</strong> <span>{0}</span>", .page_link_css)
                lht_page_link_css.Text = .page_link_css

                If Not String.IsNullOrEmpty(.link_title) Then lht_link_title.label = String.Format("<strong class='settingsSubTitle'>This is the link 'title' attribute:</strong> <span>{0}</span>", .link_title)
                lht_link_title.Text = .link_title

                If Not String.IsNullOrEmpty(.link_rel) Then lht_link_rel.label = String.Format("<strong class='settingsSubTitle'>This is the link 'rel' attribute:</strong> <span>{0}</span>", .link_rel)
                lht_link_rel.Text = .link_rel

                If Not String.IsNullOrEmpty(.page_anchor) Then lht_anchor_name.label = String.Format("<strong class='settingsSubTitle'>This can be used to name an anchor to this page:</strong> <span>{0}</span>", .page_anchor)
                lht_anchor_name.Text = .page_anchor

                ' Set visibility based on NodeType
                If Not .node_type_id = NodeType.WebPage Then
                    chk_managed_on_publish.Visible = False
                    div_meta.Visible = False
                    
                    If Not (.node_type_id = NodeType.LocalFile Or .node_type_id = NodeType.ExternalUrl) Then
                        div_anchor.Visible = False
                    End If

                End If

                ' ssl
                chk_ssl.Checked = .ssl

                ' Bind the groups repeater
                rpt_groups.DataSource = .groups
                rpt_groups.DataBind()
                If rpt_groups.Items.Count = 0 Then
                    rpt_groups.Visible = False
                Else
                    rpt_groups.Visible = True
                End If
                lhddl_groups.dropdownlist.DataSource = Group.GetGroupsAvailableToNodeID(Groove.admin_root.root_id, .node_id)
                lhddl_groups.dropdownlist.DataTextField = "name"
                lhddl_groups.dropdownlist.DataValueField = "group_id"
                lhddl_groups.DataBind()
                If lhddl_groups.dropdownlist.Items.Count = 0 Then
                    lhddl_groups.Visible = False
                Else
                    lhddl_groups.Visible = True
                End If

                ' Bind the tags repeater
                rpt_tags.DataSource = .tags
                rpt_tags.DataBind()
                If rpt_tags.Items.Count = 0 Then
                    rpt_tags.Visible = False
                Else
                    rpt_tags.Visible = True
                End If

            End With





        End Sub

#Region "Settings"


        Protected Sub lb_display_toggle_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_display_toggle.Click

            Dim n As Node = Groove.admin_current_node
            If Not n.is_default_node Then
                n.display = Not n.display
            Else
                n.display = True
            End If

            n.Update()
            BindControl()

        End Sub

        Protected Sub lb_save_pd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_save_pd.Click

            Dim n As Node = Groove.admin_current_node

            If IsDate(txt_publish_date.Text) Then
                n.publish_date = DateTime.Parse(txt_publish_date.Text)
                n.Update()
            End If

            BindControl()

        End Sub

        Protected Sub chk_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles _
            chk_display.CheckedChanged, chk_display_in_lists.CheckedChanged, chk_display_in_menus.CheckedChanged, _
            chk_archived.CheckedChanged, chk_managed_on_publish.CheckedChanged

            Dim refresh As Boolean = False

            Dim chk As CheckBox = CType(sender, CheckBox)
            Dim n As Node = Groove.admin_current_node

            Select Case chk.ID.ToLower

                Case "chk_display"
                    If Not n.is_default_node Then
                        n.display = chk_display.Checked
                    Else
                        n.display = True
                    End If


                Case "chk_display_in_lists"
                    n.display_in_lists = chk_display_in_lists.Checked

                Case "chk_display_in_menus"
                    n.display_in_menus = chk_display_in_menus.Checked

                Case "chk_archived"
                    n.archived = chk_archived.Checked

                Case "chk_managed_on_publish"

                    If n.managed_on_publish <> chk_managed_on_publish.Checked Then

                        n.managed_on_publish = chk_managed_on_publish.Checked

                        refresh = False
                        If n.managed_on_publish Then
                            refresh = NodeControl.SetCurrentPublishAssociation(n.node_id)
                            If Not refresh Then Throw New Exception("There was an error copying the current nodes controls to the working publish")

                            refresh = NodeControl.CopyCurrentPublishNodeControlsToWorkingPublish(n.node_id)
                            If Not refresh Then Throw New Exception("There was an error copying the control settings to the working publish controls")
                        Else
                            refresh = NodeControl.DeleteWorkingPublishNodeControls(n.node_id)
                            If Not refresh Then Throw New Exception("There was an error deleted the current nodes controls from the working publish")
                            refresh = NodeControl.SetCurrentPublishAssociation(n.node_id, True)
                        End If

                    End If


            End Select
            n.Update()

            If refresh Then
                Groove.RefreshPage()
            Else
                BindControl()
            End If


        End Sub

        Protected Sub lht_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _
            lht_page_title.Click, lht_meta_keywords.Click, lht_meta_description.Click, lht_meta_abstract.Click, lht_meta_author.Click, lht_meta_robots.Click, lht_meta_copyright.Click, _
            lht_menu_item_css.Click, lht_page_link_css.Click, lht_link_title.Click, lht_link_rel.Click, lht_anchor_name.Click

            Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)

            Dim n As Node = Groove.admin_current_node

            Select Case lht.ID.ToLower
                Case "lht_page_title"
                    n.page_title = lht_page_title.Text

                Case "lht_meta_keywords"
                    n.meta_keywords = lht_meta_keywords.Text

                Case "lht_meta_description"
                    n.meta_description = lht_meta_description.Text

                Case "lht_meta_abstract"
                    n.meta_abstract = lht_meta_abstract.Text

                Case "lht_meta_author"
                    n.meta_author = lht_meta_author.Text

                Case "lht_meta_robots"
                    n.meta_robots = lht_meta_robots.Text

                Case "lht_meta_copyright"
                    n.meta_copyright = lht_meta_copyright.Text



                Case "lht_menu_item_css"
                    n.menu_item_css = lht_menu_item_css.Text

                Case "lht_page_link_css"
                    n.page_link_css = lht_page_link_css.Text

                Case "lht_link_title"
                    n.link_title = lht_link_title.Text

                Case "lht_link_rel"
                    n.link_rel = lht_link_rel.Text

                Case "lht_anchor_name"
                    n.page_anchor = lht_anchor_name.Text


            End Select
            n.Update()

            BindControl()


        End Sub

        Protected Sub lhddl_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lhddl_child_component.Click, lhddl_child_layout.Click, lhddl_child_sort.Click, _
            lhddl_sitemap_url_priority.Click, lhddl_sitemap_change_frequency.Click

            Dim lhddl As LabelHiddenDropDownBox = CType(sender, LabelHiddenDropDownBox)

            Dim n As Node = Groove.admin_current_node

            Select Case lhddl.ID.ToLower
                Case "lhddl_child_component"
                    n.child_component_id = CType(lhddl_child_component.dropdownlist.SelectedValue, Integer)

                Case "lhddl_child_layout"
                    n.child_layout_node_id = CType(lhddl_child_layout.dropdownlist.SelectedValue, Integer)

                Case "lhddl_child_sort"
                    n.child_sort_type_id = CType(lhddl_child_sort.dropdownlist.SelectedValue, Integer)

                Case "lhddl_sitemap_url_priority"
                    n.sitemap_url_priority = CType(lhddl.dropdownlist.SelectedValue, Double)
                Case "lhddl_sitemap_change_frequency"
                    n.sitemap_change_frequency = lhddl.dropdownlist.SelectedValue.Substring(0, 1)
            End Select

            n.Update()


            BindControl()

        End Sub


        Protected Sub chk_ssl_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk_ssl.CheckedChanged
            Dim n As Node = Groove.admin_current_node
            n.ssl = Not n.ssl
            n.Update()
            BindControl()
        End Sub


#End Region

#Region "Images"

        Protected Sub lht_change_image_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lht_change_image.Click
            Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)
            Dim n As Node = Groove.admin_current_node

            lht.Text = lht.Text.Replace(String.Format("http://{0}", Groove.server_name), String.Empty)

            If lht_change_image.CommandArgument = "main_photo" Then
                n.main_photo = lht.Text
            Else
                n.thumbnail = lht.Text
            End If
            n.Update()           

            BindControl()

        End Sub

        Protected Sub lb_view_image_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_view_image.Click

            If lht_change_image.CommandArgument = "main_photo" Then
                lht_change_image.CommandArgument = "thumbnail"
            Else
                lht_change_image.CommandArgument = "main_photo"
            End If
            BindControl()
        End Sub

#End Region

#Region "Groups"

        Protected Sub rpt_groups_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_groups.ItemDataBound

            Dim g As Group = CType(e.Item.DataItem, Group)
            If g IsNot Nothing Then

                CType(e.Item.FindControl("lbl_group"), Label).Text = g.name

                Dim lb As LinkButton = CType(e.Item.FindControl("lb_delete_group"), LinkButton)
                lb.CommandArgument = g.group_id


            End If

        End Sub


        Protected Sub lhddl_groups_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lhddl_groups.Click
            Dim lhddl As LabelHiddenDropDownBox = CType(sender, LabelHiddenDropDownBox)
            If lhddl IsNot Nothing Then

                Group.InsertNodeIntoGroup(Groove.admin_current_node.node_id, CType(lhddl.dropdownlist.SelectedValue, Integer))

            End If
            BindControl()
        End Sub

        Protected Sub lb_delete_group_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim group_id As Integer = CType(lb.CommandArgument, Integer)
            Group.DeleteNodeFromGroup(Groove.admin_current_node.node_id, group_id)
            BindControl()
        End Sub

#End Region

#Region "Tags"

        Protected Sub rpt_tags_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_tags.ItemDataBound

            Dim t As Tag = CType(e.Item.DataItem, Tag)
            If t IsNot Nothing Then

                CType(e.Item.FindControl("lbl_tag"), Label).Text = t.name

                Dim lb As LinkButton = CType(e.Item.FindControl("lb_delete_tag"), LinkButton)
                lb.CommandArgument = t.tag_id


            End If

        End Sub


        Protected Sub lht_add_tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lht_add_tag.Click

            If Not String.IsNullOrEmpty(lht_add_tag.Text) Then

                Tag.UpdateTaggedEntityByDelineatedString(Groove.admin_root.root_id, lht_add_tag.Text, Groove.admin_current_node.entity_key, "n")

            End If
            lht_add_tag.Text = String.Empty

            BindControl()

        End Sub

        Protected Sub lb_delete_tag_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim tag_id As Integer = CType(lb.CommandArgument, Integer)

            Tag.DeleteEntityTag(Groove.admin_current_node.entity_key, tag_id)

            BindControl()
        End Sub

#End Region


    End Class

End Namespace